package com.fanrenqiji.modules.sys.service;

import com.fanrenqiji.common.persistence.Page;
import com.fanrenqiji.common.utils.StringUtils;
import com.fanrenqiji.modules.sys.dao.NotifyDao;
import com.fanrenqiji.modules.sys.dao.NotifyRecordDao;
import com.fanrenqiji.modules.sys.entity.Notify;
import com.fanrenqiji.modules.sys.entity.NotifyRecord;
import com.fanrenqiji.modules.sys.entity.Office;
import com.fanrenqiji.modules.sys.entity.User;
import com.fanrenqiji.modules.sys.utils.UserUtils;
import com.google.common.collect.Lists;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.Date;
import java.util.List;

@Service
public class NotifyService {

	@Autowired
	private NotifyDao notifyDao;

	@Autowired
	private NotifyRecordDao notifyRecordDao;

	@Autowired
	private SystemService systemService;

	public Notify get(Long id) {
		Notify entity = notifyDao.findOne(id);
		return entity;
	}

	/**
	 * 获取通知发送记录
	 * 
	 * @param notify
	 * @return
	 */
	public Notify getRecordList(Notify notify) {
		DetachedCriteria dc = notifyRecordDao.createDetachedCriteria();
		dc.createAlias("notify", "notify");
		dc.add(Restrictions.eq("notify.id", notify.getId()));
		dc.add(Restrictions.eq(NotifyRecord.DEL_FLAG,
				NotifyRecord.DEL_FLAG_NORMAL));
		notify.setNotifyRecords(notifyRecordDao.find(dc));
		return notify;
	}
	
	/**
	 * 获取当前登录人通知 NotAdmin
	 * 
	 * @param notify
	 * @return
	 */
	public Notify getCurrentUserRecordList(Notify notify) {
		DetachedCriteria dc = notifyRecordDao.createDetachedCriteria();
		dc.createAlias("notify", "notify");
		dc.add(Restrictions.eq("notify.id", notify.getId()));
		dc.add(Restrictions.eq(NotifyRecord.DEL_FLAG,
				NotifyRecord.DEL_FLAG_NORMAL));
		dc.add(Restrictions.eq("user.id",UserUtils.getUser().getId()));
		notify.setNotifyRecords(notifyRecordDao.find(dc));
		return notify;
	}
	

	public Page<Notify> find(Page<Notify> page, Notify notify) {
		User currentUser = UserUtils.getUser();
		DetachedCriteria dc = notifyDao.createDetachedCriteria();
		if (StringUtils.isNotEmpty(notify.getTitle())) {
			dc.add(Restrictions.like("title", notify.getTitle()));
		}
		if (StringUtils.isNotEmpty(notify.getType())) {
			dc.add(Restrictions.eq("type", notify.getType()));
		}
		if (StringUtils.isNotEmpty(notify.getStatus())) {
			dc.add(Restrictions.eq("status", notify.getStatus()));
		}
		if (notify.isSelf()) {
			dc.add(Restrictions.eq("status", "1"));
			dc.createCriteria("notifyRecords").add(
					Restrictions.eq("user.id", currentUser.getId()));
		}
		dc.add(Restrictions.eq(Notify.DEL_FLAG, Notify.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("updateDate"));
		Page<Notify> pages = notifyDao.find(page, dc);
		if (notify.isSelf()) {
			for (Notify notifyTemp : pages.getList()) {
				List<NotifyRecord> records = notifyTemp.getNotifyRecords();
				for (NotifyRecord record : records) {
					if (record.getUser().getId().equals(currentUser.getId())) {
						notifyTemp.setReadFlag(record.getReadFlag());
					}
				}
			}
		}
		return pages;

	}

	/**
	 * 获取通知数目
	 * 
	 * @param notify
	 * @return
	 */
	public Long findCount(Notify notify) {
		DetachedCriteria dc = notifyRecordDao.createDetachedCriteria();
		User currentUser = UserUtils.getUser();
		dc.createAlias("notify", "notify");
		dc.add(Restrictions.eq("notify.status", "1"));
		dc.add(Restrictions.eq("readFlag", "0"));
		dc.add(Restrictions.eq("user.id", currentUser.getId()));
		dc.add(Restrictions.eq(NotifyRecord.DEL_FLAG,
				NotifyRecord.DEL_FLAG_NORMAL));
		return notifyRecordDao.count(dc);
	}

	@Transactional(readOnly = false)
	public void save(Notify notify) {
		notify.setReadNum(0);
		notify.setUnReadNum(notify.getNotifyRecords().size());
		notifyDao.save(notify);
		// 更新发送接受人记录
		notifyRecordDao.deleteByNotifyId(notify.getId());
		List<NotifyRecord> notifyRecords = Lists.newArrayList();
		if (notify.getNotifyRecords().size() > 0) {
			List<NotifyRecord> notifyRecordTemp = notify.getNotifyRecords();
			for (NotifyRecord notifyRecord : notifyRecordTemp) {
				List<User> users = systemService.findUserByOffice(new Office(
						notifyRecord.getUser().getId()));
				for (User user : users) {
					notifyRecord.setUser(user);
					notifyRecords.add(notifyRecord);
				}
			}
		} else {
			List<User> users = systemService.findAllUser();
			NotifyRecord notifyRecord = null;
			for (User user : users) {
				notifyRecord = new NotifyRecord(notify);
				notifyRecord.setUser(user);
				notifyRecord.setReadFlag("0");
				notifyRecords.add(notifyRecord);
			}
		}
		notifyRecordDao.save(notifyRecords);
		notify.setUnReadNum(notifyRecords.size());
		notifyDao.save(notify);
	}

	/**
	 * 更新阅读状态
	 */
	@Transactional(readOnly = false)
	public void updateReadFlag(Notify notify) {
		User currentUser = UserUtils.getUser();
		NotifyRecord notifyRecord = notifyRecordDao.findByNotifyIdAndUserId(
				notify.getId(), currentUser.getId());
		if (notifyRecord != null) {
			if (notifyRecord.getReadFlag().equals("0")) {
				notifyRecord.setUser(UserUtils.getUser());
				notifyRecord.setReadDate(new Date());
				notifyRecord.setReadFlag("1");
				notifyRecordDao.save(notifyRecord);
				notify.setReadNum(notify.getReadNum() + 1);
				notify.setUnReadNum(notify.getUnReadNum() - 1);
				notifyDao.save(notify);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(Notify notify) {
		notifyRecordDao.fakeDeleteByNotifyId(notify.getId());
		notifyDao.deleteById(notify.getId());
	}
}
